package com.wj.ballcat.demo.controller;

import com.hccake.ballcat.common.security.util.SecurityUtils;
import com.wj.ballcat.demo.model.entity.Demo;
import com.wj.ballcat.demo.model.qo.DemoQO;
import com.wj.ballcat.demo.model.vo.DemoPageVO;
import com.wj.ballcat.demo.service.DemoService;
import com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging;
import com.hccake.ballcat.common.model.domain.PageParam;
import com.hccake.ballcat.common.model.domain.PageResult;
import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.common.model.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ballcat 2022-10-08 17:08:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/demo/demo")
@Tag(name = "管理")
public class DemoController {

	private final DemoService demoService;

	/**
	 * 分页查询
	 * @param pageParam 分页参数
	 * @param demoQO 查询对象
	 * @return R 通用返回体
	 */
	@Operation(summary = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@per.hasPermission('demo:demo:read')")
	public R<PageResult<DemoPageVO>> getDemoPage(PageParam pageParam, DemoQO demoQO) {
		return R.ok(demoService.queryPage(pageParam, demoQO));
	}

	/**
	 * 新增
	 * @param demo
	 * @return R 通用返回体
	 */
	@Operation(summary = "新增")
	@CreateOperationLogging(msg = "新增")
	@PostMapping
	@PreAuthorize("@per.hasPermission('demo:demo:add')")
	public R<Void> save(@RequestBody Demo demo) {
		return demoService.save(demo) ? R.ok() : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新增失败");
	}

	/**
	 * 修改
	 * @param demo
	 * @return R 通用返回体
	 */
	@Operation(summary = "修改")
	@UpdateOperationLogging(msg = "修改")
	@PutMapping
	@PreAuthorize("@per.hasPermission('demo:demo:edit')")
	public R<Void> updateById(@RequestBody Demo demo) {
		System.out.println("11111"+SecurityUtils.getUser().getName());
		return demoService.updateById(demo) ? R.ok() : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "修改失败");
	}

	/**
	 * 通过id删除
	 * @param id id
	 * @return R 通用返回体
	 */
	@Operation(summary = "通过id删除")
	@DeleteOperationLogging(msg = "通过id删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@per.hasPermission('demo:demo:del')")
	public R<Void> removeById(@PathVariable("id") String id) {
		return demoService.removeById(id) ? R.ok() : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "通过id删除失败");
	}

}
